package com.cnc.servlet;

import com.cnc.entity.Student;
import com.cnc.service.Impl.StudentServiceImpl;
import com.cnc.service.StudentService;
import com.cnc.utils.PageUtil;
import com.cnc.utils.ResourceUtil;
import com.cnc.utils.SearchUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@WebServlet("/vue")
public class VueServlet extends BaseServlet {
    StudentService service=new StudentServiceImpl();
    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        int rows = ResourceUtil.getRows();
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String min = req.getParameter("Min");
        String max = req.getParameter("Max");
        SearchUtil su = new SearchUtil(name, max, min, gender);
        int countRows = service.getCountRows(su);
        PageUtil pu = new PageUtil(page, rows, countRows);
        List<Student> list =service.searchStudent(su,pu);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pu",pu);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.close();
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sids = req.getParameter("sids");
        String sid = req.getParameter("sid");
        if(sids!=null){
            if(!sids.equals("")){
                String[] split = sids.split(",");
                for (String s : split) {
                    service.deleteStudent(Integer.parseInt(s));
                }
            }
        }else if(sid!=null&&!sid.equals("")){
            boolean rows = service.deleteStudent((Integer.valueOf(req.getParameter("sid"))));
        }
        resp.sendRedirect("StudentServlet?flag=show");
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        int sage = Integer.valueOf(req.getParameter("sage"));
        String sgender = req.getParameter("sgender");
        String semail = req.getParameter("semail");
        String filename;
        if(sid!=null){
            Part photo = req.getPart("photo");
            if(photo.getSubmittedFileName().equals("")){
                filename=ResourceUtil.getImg();
            }else {
                filename=photo.getSubmittedFileName();
                File file = new File(ResourceUtil.getAbsPath() + filename);
                if(!file.exists()){
                    photo.write(ResourceUtil.getAbsPath() + filename);
                }
            }
        }else {
            sid=String.valueOf(service.getMaxId()+1);
            filename=ResourceUtil.getImg();
        }
        boolean rows = service.insertStudent(new Student(Integer.valueOf(sid), sname, sage, sgender, semail,filename));
        int row = ResourceUtil.getRows();
        int countRows = service.getCountRows(new SearchUtil(null,null,null,null));
        PageUtil pu = new PageUtil("1", row, countRows);
        resp.sendRedirect("StudentServlet?flag=show&page="+pu.getCountPage());
    }

    protected void updateByKey(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        String key = req.getParameter("key");
        String value = req.getParameter("value");
        service.updateStudent(Integer.valueOf(sid), key, value);
        resp.sendRedirect("StudentServlet?flag=show");
    }
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String sage = req.getParameter("sage");
        String sgender = req.getParameter("sgender");
        String semail = req.getParameter("semail");
        String oldPhoto = req.getParameter("oldPhoto");
        Part photo = req.getPart("photo");
        String fileName = photo.getSubmittedFileName();
        if(fileName.equals("")){
            fileName=oldPhoto;
        }else {
            File file = new File(ResourceUtil.getAbsPath() + fileName);
            if(!file.exists()){
                photo.write(ResourceUtil.getAbsPath() + fileName);
            }
        }
        boolean result=service.editStudent(new Student(Integer.valueOf(sid),sname,Integer.valueOf(sage),sgender,semail,fileName));
        resp.sendRedirect("StudentServlet?flag=show");
    }
}
